package Dao.daoImpl;

import Dao.empDao;
import Model.Emp;
import org.junit.Test;

import java.sql.*;

public class EmpDaoImpl implements empDao {
    static Connection preconn = null;
    PreparedStatement preparedStatement = null;


    static {
        try {
            preconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2024", "root", "123456");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    //删除
    @Test
    public void delEmpDao(int empId) throws SQLException {
        String sql = "delete from Emp where empId=?";
        preparedStatement = preconn.prepareStatement(sql);
        preparedStatement.setInt(1, empId);

        int i = preparedStatement.executeUpdate();
        System.out.println("i = " + i);
//        close();
    }

    //查询
    /*
       @param emp
          @param len
     */

    @Test
    public void showEmpDao() throws SQLException {
        String sql = "select * from Emp";
        preparedStatement = preconn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        Emp emp = new Emp();
        while (resultSet.next()) {
            Integer empId = resultSet.getInt("empId");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            Integer age = resultSet.getInt("age");
            String job = resultSet.getString("job");

            System.out.println("empId = " + empId);
            System.out.println("name = " + name);
            System.out.println("sex = " + sex);
            System.out.println("age = " + age);
            System.out.println("job = " + job);

            emp.setEmpId(empId);
            emp.setName(name);
            emp.setSex(sex);
            emp.setAge(age);
            emp.setJob(job);
        }
    }

    //修改
    @Test
    public boolean updateEmpDao(Emp obj, int empId1) throws SQLException {
        String sql = "update Emp set name=?,sex=?,age=?,job=? where empId=?";
        preparedStatement = preconn.prepareStatement(sql);

        preparedStatement.setString(1, obj.getName());
        preparedStatement.setString(2, obj.getSex());
        preparedStatement.setInt(3, obj.getAge());
        preparedStatement.setString(4, obj.getJob());
        preparedStatement.setInt(5, empId1);
        int i = preparedStatement.executeUpdate();
        System.out.println("i = " + i);
        return true;
    }

    /*增加
              数据访问层 添加员工信息
              @param emp Array 数据集合 地址
              @param user 一条员工信息
           */
    @Test
    public void addEmpDao(Emp obj) throws SQLException {
        String sql = "insert into Emp values (null,?,?,?,?)";
        preparedStatement = preconn.prepareStatement(sql);

        preparedStatement.setString(1, obj.getName());
        preparedStatement.setString(2, obj.getSex());
        preparedStatement.setInt(3, obj.getAge());
        preparedStatement.setString(4, obj.getJob());

        int i = preparedStatement.executeUpdate();
        System.out.println(i);
//        close();

    }

    /**
     * 根据ID查询一条数据
     *
     * @throws SQLException
     */
    public Emp findOneDataDao(int id) throws SQLException {
        String sql = "select * from Emp where empId=? limit 1";
        preparedStatement = preconn.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        Emp emp = new Emp();
        if (resultSet.next()) {
            Integer empId = resultSet.getInt("empId");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            Integer age = resultSet.getInt("age");
            String job = resultSet.getString("job");
            emp.setEmpId(empId);
            emp.setName(name);
            emp.setSex(sex);
            emp.setAge(age);
            emp.setJob(job);
            return emp;
        } else {
            return null;
        }
    }

    public void close() throws SQLException {
        preconn.close();
        preparedStatement.close();
    }
}
